<?php
/**
 * excel 导出用户信息
 */
require_once(dirname(__FILE__)."/config.php");

include_once DEDEINC.'/phpexcel/Classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();

$objActSheet = $objPHPExcel->getActiveSheet();

$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setLastModifiedBy("Maarten Balliauw")
    ->setTitle("Office 2007 XLSX Test Document")->setSubject("Office 2007 XLSX Test Document")
    ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")
    ->setCategory("Test result file");

$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', 'username')
    ->setCellValue('B1', 'userface')
    ->setCellValue('C1', 'userpwd')
    ->setCellValue('D1', 'usersex')
    ->setCellValue('E1', 'isvip')
    ->setCellValue('F1', 'viptime')
    ->setCellValue('G1', 'vipdate')
    ->setCellValue('H1', 'wxopenid')
    ->setCellValue('I1', 'wxunionid')
    ->setCellValue('J1', 'user_email')
    ->setCellValue('K1', 'score')
    ->setCellValue('L1', 'create_time');

$row = 10000;
$page = ($_GET['page'] - 1) * $row;

$member_sql = "SELECT * FROM `#@__member` limit {$page}, {$row}";

$dsql->SetQuery($member_sql);
$dsql->Execute();

$index = 1;

while ($row = $dsql->GetArray()) {

    if ($row['sex'] == '男') {
        $sex = 0;
    } else {
        $sex = 1;
    }

    $vip_time = $row['uptime'];

    $vip_end = '';

    $is_vip = 0;

    if ($row['exptime']) {
        $date = date_create(date('Y-m-d', $vip_time));
        date_add($date, date_interval_create_from_date_string("{$row['exptime']} days"));
        $end_time = date_format($date, "Y-m-d 23:59:59");
        $vip_end = strtotime($end_time);

        if ($vip_end && $end_time > date('2022-04-04')) {
            $is_vip = 1;
        }
    }

    $index++;

    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A' .
            $index, $row['uname'])->setCellValue('B' .
            $index, $row['face'])->setCellValue('C' .
            $index, $row['pwd'])->setCellValue('D' .
            $index, $sex)->setCellValueExplicit('E' .
            $index, $is_vip)->setCellValue('F' .
            $index, $row['uptime'])->setCellValue('G' .
            $index, $vip_end)->setCellValue('H' .
            $index, $row['openid'])->setCellValue('I' .
            $index, $row['unionid'])->setCellValue('J' .
            $index, $row['email'])->setCellValue('K' .
            $index, $row['scores'])->setCellValue('L' .
            $index, $row['jointime']);
}

// Rename worksheetwww.dedebase.com
$objPHPExcel->getActiveSheet()->setTitle('Simple');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="ltpmx.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory :: createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

dd($page.'已导出完成~');


